package com.dunqi.gpm.cangxi.home.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseFragment;
import com.dunqi.gpm.cangxi.common.util.SwipeUtil;
import com.dunqi.gpm.cangxi.home.activity.ProjectProgressDetailsActivity;
import com.dunqi.gpm.cangxi.home.activity.WarningAuditActivity;
import com.dunqi.gpm.cangxi.home.adapter.ProjectTimelineAdapter;
import com.dunqi.gpm.cangxi.home.bean.Page;
import com.dunqi.gpm.cangxi.home.bean.ProjectTimeline;
import com.dunqi.gpm.cangxi.home.widget.EmptyView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by 空 on 2017/11/22 0022.
 */

public class ReportAuditFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;

    private ProjectTimelineAdapter adapter;
    List<ProjectTimeline> data = new ArrayList<>();
//    private Project project;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .queryOwnedProgressList(false, 1, page)
                .compose(RxSchedulers.<Page<ProjectTimeline>>io_main())
                .subscribe(new Consumer<Page<ProjectTimeline>>() {
                    @Override
                    public void accept(Page<ProjectTimeline> projectTimelinePage) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);
                        ArrayList<ProjectTimeline> list = projectTimelinePage.getList();

                        data.clear();

                        if (page == 1) {
                            data.clear();
                        }

                        data.addAll(list);

                        adapter.notifyDataSetChanged();

                        if (projectTimelinePage.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd(true);
                        }

                        page++;

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        adapter.loadMoreFail();
                        SwipeUtil.loadCompleted(swipeRefresh);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        SwipeUtil.init(swipeRefresh);

        recycler.setLayoutManager(new LinearLayoutManager(context));

        adapter = new ProjectTimelineAdapter(context, data);
        recycler.setAdapter(adapter);
        adapter.setEmptyView(new EmptyView(context));
        swipeRefresh.setOnRefreshListener(this);
        adapter.setOnLoadMoreListener(this, recycler);

        adapter.setOnItemClickListener(this);


    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_time_line;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static ReportAuditFragment newInstance() {
        ReportAuditFragment fragment = new ReportAuditFragment();
        return fragment;
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ProjectProgressDetailsActivity.start((Activity) context, data.get(position).getProPlanProgressId());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == WarningAuditActivity.RESULT_CODE) {
            onRefresh();
        }
    }
}
